package yl.hs.bmipfull.service.finance.impl;

import org.springframework.beans.factory.annotation.Autowired;
import yl.hs.bmipfull.annotations.DBTable;
import yl.hs.bmipfull.daos.finance.IFinanceSystemLogDao;
import yl.hs.bmipfull.pojo.finance.FinanceSystemLogEntity;
import yl.hs.bmipfull.service.finance.IFinanceService;
import yl.hs.bmipfull.service.impl.BaseService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class FinanceService extends BaseService implements IFinanceService {
    @Autowired
    private IFinanceSystemLogDao logDao;
    @Override
    public Integer writeLog(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {

        var user = HsApp.getCurrent();
        var log = new FinanceSystemLogEntity();
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        String objectName = "信息";
        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }


        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        } else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        log.setUserSN(userSN);
        switch (action) {
            case PRINT -> content = String.format("%s打印了编号为%s的%s信息", userName, dataSN, objectName);
            case CREATE -> content = String.format("%s创建了编号为%s的%s信息", userName, dataSN, objectName);
            case UPDATE -> content = String.format("%s更新了编号为%s的%s信息:%s", userName, dataSN, objectName, content);
            case DELETE -> content = String.format("%s删除了编号为%s的%s信息", userName, dataSN, objectName);
            case EXPORT -> content = String.format("%s导出了编号为%s的%s信息", userName, dataSN, objectName);
            case IMPORT -> content = String.format("%s导入了编号为%s的%s信息", userName, dataSN, objectName);
            case QUERY -> content = String.format("%s查询了编号为%s的%s信息", userName, dataSN, objectName);
            default -> content = String.format("%s操作了编号为%s的%s信息", userName, dataSN, objectName);
        }
        log.setContent(content);
        log.setDataSN(dataSN);
        log.setActionType(action.code);
        //SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.setHandleTime(new Date());
        log.setUserName(userName);
        return logDao.insertNew(log);
    }

    @Override
    public Integer writeLogs(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        return null;
    }

    @Override
    public void forWriteLog(String[] snList, short state) {
        var user = HsApp.getCurrent();
        if (state == -2) {
            for (var str:snList) {
                writeLog(ActionType.UPDATE,str,user.getUserName()+"彻底删除了数据编号为"+str+"的数据信息","",null);
            }
        }else{
            for (var str:snList) {
                writeLog(ActionType.UPDATE,str,user.getUserName()+"恢复删除了数据编号为"+str+"的数据信息","",null);
            }
        }
    }
    protected Date parseTime(String date, boolean start) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (!Tl.isEmpty(date)) {
            if (start) {
                return sdf.parse(date);
            }
            var time = sdf.parse(date);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(time);
            calendar.add(Calendar.MINUTE, 2);
            return calendar.getTime();
        } else if (start) {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(new Date());
            calendar.add(Calendar.YEAR, -1);
            return calendar.getTime();
        } else {
            return new Date();
        }
    }
}
